package com.lin.springbootexceptionHandling.controller;

import com.lin.springbootexceptionHandling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 异常处理器测试, 该类中定义了局部异常处理器, 不管有没有全局处理器,
 * 都会走这个局部处理器, 该处理器优先级高于全局处理器
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list/{id}")
    public String getUserList(@PathVariable int id) {
        if (id == 1) throw new IllegalArgumentException("Id is invalid");
        return userService.getUserList().toString();
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public String exceptionHandler(Exception e) {
        return "UserController 局部异常处理器: \r\n" + e.getMessage();
    }
}
